package com.stlm2.dems.service;

import com.stlm2.dems.dao.DdmGoodsDao;
import com.stlm2.dems.dao.GoodsCategoryDao;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.*;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.dao.GoodsRelateCategoryDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("goodsRelateCategoryService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class GoodsRelateCategoryService extends BaseServiceImpl<GoodsRelateCategory> {

	@Resource
	private GoodsRelateCategoryDao goodsRelateCategoryDao;
	@Resource
	private GoodsCategoryDao goodsCategoryDao;
	@Resource
	private DdmGoodsDao ddmGoodsDao;
	@Resource
	private UserDao userDao;

	@Override
	protected BaseDao <GoodsRelateCategory> getDao() {
		return goodsRelateCategoryDao;
	}

	/**crm
	 * 添加商品 分类对应关系记录
	 * ffl
	 * 2017-11-01 11:32:55
	 * @param token
	 * @param levelOneId
	 * @param levelTwoId
	 * @param levelThreeId
	 * @param goodsId
	 * @return
	 */
	public  String addGoodsRelateCategory(String token,String levelOneId,String levelTwoId,String levelThreeId,String goodsId){
		User user=userDao.getByToken(token);
		if(user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		if (!NumberUtils.isDigits(levelOneId)){
			return CodeContent.getModelByCode(0,"未传入合法一级分类id",null);
		}
		GoodsCategory firstCategory=goodsCategoryDao.getById(NumberUtils.toInt(levelOneId));

		if (!NumberUtils.isDigits(levelTwoId)){
			return CodeContent.getModelByCode(0,"未传入合法二级分类id",null);
		}
		GoodsCategory secondCategory=goodsCategoryDao.getById(NumberUtils.toInt(levelTwoId));

		if (!NumberUtils.isDigits(levelThreeId)){
			return CodeContent.getModelByCode(0,"未传入合法三级分类id",null);
		}
		GoodsCategory thirdCategory=goodsCategoryDao.getById(NumberUtils.toInt(levelThreeId));

		if (!NumberUtils.isDigits(goodsId)){
			return CodeContent.getModelByCode(0,"未传入合法商品id",null);
		}
		DdmGoods goods=ddmGoodsDao.getById(NumberUtils.toInt(goodsId));
		if(goods==null){
			return CodeContent.getModelByCode(0,"未找到商品",null);
		}
		if(firstCategory==null){
			return CodeContent.getModelByCode(0,"未找到一级分类",null);
		}
		if(secondCategory==null){
			return CodeContent.getModelByCode(0,"未找到二级分类",null);
		}
		if(thirdCategory==null){
			return CodeContent.getModelByCode(0,"未找到三级分类",null);
		}

		if (firstCategory.getGoodsCategoryLevelValue()-1!=0){
			return CodeContent.getModelByCode(0,"根据所传入的一级分类id查询到的分类不是一级分类",null);
		}
		if (secondCategory.getGoodsCategoryLevelValue()-2!=0){
			return CodeContent.getModelByCode(0,"根据所传入的二级分类id查询到的分类不是二级分类",null);
		}
		if (thirdCategory.getGoodsCategoryLevelValue()-3!=0){
			return CodeContent.getModelByCode(0,"根据所传入的三级分类id查询到的分类不是三级分类",null);
		}
		if(firstCategory.getId()-secondCategory.getParentCategoryId()!=0){
			return CodeContent.getModelByCode(0,"查询到的二级分类的父级分类不对应传入的一级分类",null);
		}
		if(secondCategory.getId()-thirdCategory.getParentCategoryId()!=0){
			return CodeContent.getModelByCode(0,"查询到的三级分类的父级分类不对应传入的二级分类",null);
		}

		GoodsRelateCategory goodsRelateCategory=new GoodsRelateCategory();
		goodsRelateCategory.setCreateTime(new Date());
		goodsRelateCategory.setCreateUserId(user.getId());
		goodsRelateCategory.setIsValid(true);
		goodsRelateCategory.setGoodsId(goods.getId());
		GoodsRelateCategory oldThree=goodsRelateCategoryDao.getByGoodsIdAndCategory(NumberUtils.toInt(goodsId),thirdCategory.getId());
		if(oldThree!=null){//已添加
			return CodeContent.getModelByCode(1,"",null);
		}else {//未添加
			goodsRelateCategory.setGoodsCategoryId(thirdCategory.getId());
			goodsRelateCategory.setCategoryName(thirdCategory.getName());
			goodsRelateCategory.setGoodsCategoryLevel(thirdCategory.getGoodsCategoryLevel());
			goodsRelateCategoryDao.add(goodsRelateCategory);
		}
		GoodsRelateCategory oldTwo=goodsRelateCategoryDao.getByGoodsIdAndCategory(NumberUtils.toInt(goodsId),secondCategory.getId());
		if(oldTwo==null){//未添加
			goodsRelateCategory.setGoodsCategoryId(secondCategory.getId());
			goodsRelateCategory.setCategoryName(secondCategory.getName());
			goodsRelateCategory.setGoodsCategoryLevel(secondCategory.getGoodsCategoryLevel());
			goodsRelateCategoryDao.add(goodsRelateCategory);
		}
		GoodsRelateCategory oldOne=goodsRelateCategoryDao.getByGoodsIdAndCategory(NumberUtils.toInt(goodsId),firstCategory.getId());
		if(oldOne ==null){//未添加
			goodsRelateCategory.setGoodsCategoryId(firstCategory.getId());
			goodsRelateCategory.setCategoryName(firstCategory.getName());
			goodsRelateCategory.setGoodsCategoryLevel(firstCategory.getGoodsCategoryLevel());
			goodsRelateCategoryDao.add(goodsRelateCategory);
		}



		return CodeContent.getModelByCode(1,"",null);
	}

	/**crm
	 *获取商品 的分类
	 * ffl
	 * 2017-11-01 15:32:09
	 * @param token
	 * @param goodsId
	 * @return
	 */
	public String  getGoodsRelateCategory(String token,String goodsId){
		User user=userDao.getByToken(token);
		if(user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		if (!NumberUtils.isDigits(goodsId)){
			return CodeContent.getModelByCode(0,"未传入合法商品id",null);
		}
        List<GoodsRelateCategory> goodsRelateCategoryList=goodsRelateCategoryDao.getByGoodsIdAndLevel(NumberUtils.toInt(goodsId),3);
		return CodeContent.getModelByCode(1,"",convert_third(goodsRelateCategoryList));
	}

	public List<ModelMap> convert_third(List<GoodsRelateCategory> goodsRelateCategoryList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (goodsRelateCategoryList != null && goodsRelateCategoryList.size() > 0) {
			for (GoodsRelateCategory goodsRelateCategory : goodsRelateCategoryList) {
				ModelMap map = buildModel_third(goodsRelateCategory);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModel_third(GoodsRelateCategory goodsRelateCategory) {
		ModelMap model = new ModelMap();
		GoodsCategory thirdLevel=goodsCategoryDao.getById(goodsRelateCategory.getGoodsCategoryId());
		model.put("id",goodsRelateCategory.getId());
		model.put("thirdName",goodsRelateCategory.getCategoryName());
		if(thirdLevel!=null){
			GoodsRelateCategory levelTwo=goodsRelateCategoryDao.getByGoodsIdAndCategory(goodsRelateCategory.getGoodsId(),thirdLevel.getParentCategoryId());
			if(levelTwo!=null){
				GoodsCategory secondLevel=goodsCategoryDao.getById(levelTwo.getGoodsCategoryId());
				model.put("secondName",levelTwo.getCategoryName());
				if(secondLevel!=null){
					GoodsRelateCategory levelOne=goodsRelateCategoryDao.getByGoodsIdAndCategory(goodsRelateCategory.getGoodsId(),secondLevel.getParentCategoryId());
					if(levelOne!=null){
						model.put("firstName",levelOne.getCategoryName());
					}
				}
			}
		}
		return  model;
	}

	/**crm
	 * 商品关联商品分类记录无效 掉
	 * ffl
	 * 2017-11-01 15:52:37
	 * @param token
	 * @param levelOneId
	 * @param levelTwoId
	 * @param levelThreeId
	 * @param goodsId
	 * @return
	 */
	public String  setNotValid(String token,String levelOneId,String levelTwoId,String levelThreeId,String goodsId)  {
		User user=userDao.getByToken(token);
		if(user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		if (!NumberUtils.isDigits(levelOneId)){
			return CodeContent.getModelByCode(0,"未传入合法一级分类id",null);
		}
		GoodsCategory firstCategory=goodsCategoryDao.getById(NumberUtils.toInt(levelOneId));

		if (!NumberUtils.isDigits(levelTwoId)){
			return CodeContent.getModelByCode(0,"未传入合法二级分类id",null);
		}
		GoodsCategory secondCategory=goodsCategoryDao.getById(NumberUtils.toInt(levelTwoId));
		if (!NumberUtils.isDigits(levelThreeId)){
			return CodeContent.getModelByCode(0,"未传入合法三级分类id",null);
		}
		GoodsCategory thirdCategory=goodsCategoryDao.getById(NumberUtils.toInt(levelThreeId));
		if (!NumberUtils.isDigits(goodsId)){
			return CodeContent.getModelByCode(0,"未传入合法商品id",null);
		}
		DdmGoods goods=ddmGoodsDao.getById(NumberUtils.toInt(goodsId));
		if(goods==null){
			return CodeContent.getModelByCode(0,"未找到商品",null);
		}
		if(firstCategory==null){
			return CodeContent.getModelByCode(0,"未找到一级分类",null);
		}
		if(secondCategory==null){
			return CodeContent.getModelByCode(0,"未找到二级分类",null);
		}
		if(thirdCategory==null){
			return CodeContent.getModelByCode(0,"未找到三级分类",null);
		}

		if (firstCategory.getGoodsCategoryLevelValue()-1!=0){
			return CodeContent.getModelByCode(0,"根据所传入的一级分类id查询到的分类不是一级分类",null);
		}
		if (secondCategory.getGoodsCategoryLevelValue()-2!=0){
			return CodeContent.getModelByCode(0,"根据所传入的二级分类id查询到的分类不是二级分类",null);
		}
		if (thirdCategory.getGoodsCategoryLevelValue()-3!=0){
			return CodeContent.getModelByCode(0,"根据所传入的三级分类id查询到的分类不是三级分类",null);
		}
		if(firstCategory.getId()-secondCategory.getParentCategoryId()!=0){
			return CodeContent.getModelByCode(0,"查询到的二级分类的父级分类不对应传入的一级分类",null);
		}
		if(secondCategory.getId()-thirdCategory.getParentCategoryId()!=0){
			return CodeContent.getModelByCode(0,"查询到的三级分类的父级分类不对应传入的二级分类",null);
		}
		GoodsRelateCategory levelOne=goodsRelateCategoryDao.getByGoodsIdAndCategory(NumberUtils.toInt(goodsId),NumberUtils.toInt(levelOneId));

		GoodsRelateCategory levelTwo=goodsRelateCategoryDao.getByGoodsIdAndCategory(NumberUtils.toInt(goodsId),NumberUtils.toInt(levelTwoId));

		GoodsRelateCategory levelThree=goodsRelateCategoryDao.getByGoodsIdAndCategory(NumberUtils.toInt(goodsId),NumberUtils.toInt(levelThreeId));

		if(levelThree!=null){
			levelThree.setIsValid(false);
			goodsRelateCategoryDao.modify(levelThree);
			//判断 三级分类的父分类   下的还有没有有效三级分类 有 不去无效操作  无 进行无效操作

			List<GoodsRelateCategory> listThree=goodsRelateCategoryDao.getByCategoryIdAndGoodsId(thirdCategory.getParentCategoryId(),levelThree.getGoodsId());
			if(listThree!=null&&listThree.size()>0){
				//判断 三级分类的父分类   下的还有有效三级分类  不去无效操作
			}else {
				//判断 三级分类的父分类   下的没有有效三级分类  进行无效操作
				if (levelTwo!=null){
					levelTwo.setIsValid(false);
					goodsRelateCategoryDao.modify(levelTwo);
					//判断 二级级分类的父分类   下的还有没有有效二级分类 有 不去无效操作  无 进行无效操作
					List<GoodsRelateCategory> listTwo=goodsRelateCategoryDao.getByCategoryIdAndGoodsId(secondCategory.getParentCategoryId(),levelThree.getGoodsId());
					if(listTwo!=null&&listTwo.size()>0){
						//判断 二级级分类的父分类   下的还有有效二级分类  不去无效操作
					}else {
						//判断 二级级分类的父分类   下的没有有效二级分类  进行无效操作
						if(levelOne!=null){
							levelOne.setIsValid(false);
							goodsRelateCategoryDao.modify(levelOne);
						}
					}
				}
			}
		}
		return CodeContent.getModelByCode(1,"",null);
	}

	/**crm
	 * 商品关联商品分类记录无效掉
	 * ffl
	 * 2017-11-01 15:52:37
	 * @param token
	 * @param id
	 * @return
	 */
	public String  setNotValid(String token,String id)  {
		User user=userDao.getByToken(token);
		if(user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}

		if (!NumberUtils.isDigits(id)){
			return CodeContent.getModelByCode(0,"未传入合法分类id",null);
		}
		GoodsRelateCategory levelThree=goodsRelateCategoryDao.getById(NumberUtils.toInt(id));
		GoodsCategory thirdCategory=goodsCategoryDao.getById(levelThree.getGoodsCategoryId());
		if(thirdCategory==null){
			return CodeContent.getModelByCode(0,"未找到三级分类",null);
		}

		GoodsCategory secondCategory=goodsCategoryDao.getById(thirdCategory.getParentCategoryId());
		if(secondCategory==null){
			return CodeContent.getModelByCode(0,"未找到二级分类",null);
		}

		GoodsCategory firstCategory=goodsCategoryDao.getById(secondCategory.getParentCategoryId());
		if(firstCategory==null){
			return CodeContent.getModelByCode(0,"未找到一级分类",null);
		}


		if (firstCategory.getGoodsCategoryLevelValue()-1!=0){
			return CodeContent.getModelByCode(0,"根据所传入的一级分类id查询到的分类不是一级分类",null);
		}
		if (secondCategory.getGoodsCategoryLevelValue()-2!=0){
			return CodeContent.getModelByCode(0,"根据所传入的二级分类id查询到的分类不是二级分类",null);
		}
		if (thirdCategory.getGoodsCategoryLevelValue()-3!=0){
			return CodeContent.getModelByCode(0,"根据所传入的三级分类id查询到的分类不是三级分类",null);
		}
		if(firstCategory.getId()-secondCategory.getParentCategoryId()!=0){
			return CodeContent.getModelByCode(0,"查询到的二级分类的父级分类不对应传入的一级分类",null);
		}
		if(secondCategory.getId()-thirdCategory.getParentCategoryId()!=0){
			return CodeContent.getModelByCode(0,"查询到的三级分类的父级分类不对应传入的二级分类",null);
		}
		GoodsRelateCategory levelOne=goodsRelateCategoryDao.getByGoodsIdAndCategory(levelThree.getGoodsId(),firstCategory.getId());

		GoodsRelateCategory levelTwo=goodsRelateCategoryDao.getByGoodsIdAndCategory(levelThree.getGoodsId(),secondCategory.getId());

		if(levelThree!=null){
			goodsRelateCategoryDao.delete(levelThree.getId());
			//判断 三级分类的父分类   下的还有没有有效三级分类 有 不去删除操作  无 进行删除操作
			List<GoodsRelateCategory> listThree=goodsRelateCategoryDao.getByCategoryIdAndGoodsId(thirdCategory.getParentCategoryId(),levelThree.getGoodsId());
			if(listThree!=null&&listThree.size()>0){
				//判断 三级分类的父分类   下的还有有效三级分类  不去删除操作
			}else {
				//判断 三级分类的父分类   下的没有有效三级分类  进行无效操作
				if (levelTwo!=null){
					goodsRelateCategoryDao.delete(levelTwo.getId());
					//判断 二级级分类的父分类   下的还有没有有效二级分类 有 不去删除操作  无 进行删除操作
					List<GoodsRelateCategory> listTwo=goodsRelateCategoryDao.getByCategoryIdAndGoodsId(secondCategory.getParentCategoryId(),levelThree.getGoodsId());
					if(listTwo!=null&&listTwo.size()>0){
						//判断 二级级分类的父分类   下的还有有效二级分类  不去删除操作
					}else {
						//判断 二级级分类的父分类   下的没有有效二级分类  进行删除操作
						if(levelOne!=null){
							goodsRelateCategoryDao.delete(levelOne.getId());
						}
					}
				}
			}
		}
		return CodeContent.getModelByCode(1,"",null);
	}


}